﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagedDLL;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using Microsoft.Xna.Framework;
using DualityEngine.Business;

namespace DualityEngine.Data
{
    public class ServerConnector
    {
        private static readonly ServerConnector instance = new ServerConnector();
        private TcpClient myclient;
        private NetworkStream networkstream;
        private BinaryWriter streamwriter;
        private Thread readincomingmessagesthread;

        //Rendering
        public Dictionary<string, PlayerInfo> playerrendering;

        public static ServerConnector Instance
        {
            get
            {
                return instance;
            }
        }

        private ServerConnector()
        {
            playerrendering = new Dictionary<string, PlayerInfo>();
            try
            {
                myclient = new TcpClient("localhost", 1234);
                networkstream = myclient.GetStream();
                streamwriter = new BinaryWriter(networkstream);
                readincomingmessagesthread = new Thread(this.ReadFromServer);
                readincomingmessagesthread.Start();

                //Aanmelden bij server
                DataPackageObject d = new DataPackageObject();
                d.parameters = new object[2];
                d.parameters[0] = 0;
                d.parameters[1] = "El_Stupido";
                streamwriter.Write(ObjectSerializer.Serialize(d));
                streamwriter.Flush();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void SendChatMessageToServer(string message)
        {
            DataPackageObject d = new DataPackageObject();
            d.parameters = new object[2];
            d.parameters[0] = 4;
            d.parameters[1] = message;

            streamwriter.Write(ObjectSerializer.Serialize(d));
            streamwriter.Flush();
        }

        public void SendPlayerInfo(Vector3 shipposition, Vector3 direction, Vector3 up, Vector3 right)
        {
            DataPackageObject d = new DataPackageObject();
            d.parameters = new object[3];
            d.parameters[0] = 5;
            d.parameters[1] = new PlayerInfo() { position = new VectorObject(shipposition.X, shipposition.Y, shipposition.Z), direction = new VectorObject(direction.X, direction.Y, direction.Z), rightvector = new VectorObject(right.X, right.Y, right.Z), upvector = new VectorObject(up.X, up.Y, up.Z)};

            streamwriter.Write(ObjectSerializer.Serialize(d));
            streamwriter.Flush();
        }

        private void ReadFromServer()
        {
            while (true)
            {
                byte[] receivedline = new byte[myclient.ReceiveBufferSize];
                networkstream.Read(receivedline, 0, (int)myclient.ReceiveBufferSize);
                networkstream.Flush();
                DataPackageObject d = ObjectSerializer.Deserialize(receivedline);
                HandleMessage(d);
            }
        }

        private void HandleMessage(DataPackageObject d)
        {
            switch ((int)d.parameters[0])
            {
                case 1:
                    //Receive Console Message
                    ChatManager.Instance.AddMessage((string)d.parameters[1] + " say: " + (string)d.parameters[2]);
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    //Receive Player info
                    playerrendering[(string)d.parameters[1]] = (PlayerInfo)d.parameters[2];                    
                    break;
            }
        }
    }
}
